package cn.edu360.streaming.etl;

import cn.edu360.streaming.model.NginxLog;
import cn.edu360.streaming.utils.DateUtil;
import cn.edu360.streaming.utils.GropUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.ValueFilter;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * wzxjava@126.com
 * Created by wangzhixuan on 2017/05/18 22:22
 */
public class NginxLogEtl implements Serializable {

    private static final long serialVersionUID = 2834157757509871165L;

    public static String jsonToHiveString(String message, String reg, Map<String, String> defaultReg) {
        JSONObject mg = JSON.parseObject(message);
        String info = mg.getString("message");
        HashMap<String, String> map = GropUtil.stringToJson(info, reg, defaultReg);
        ValueFilter valueFilter = new ValueFilter() {
            @Override
            public Object process(Object o, String propertyName, Object propertyValue) {
                if (propertyName.equals("time_local")) {
                    // 返回修改后的属性值对象
                    return DateUtil.nginxDate(propertyValue.toString());
                }
                return propertyValue;
            }
        };
        String json = JSON.toJSONString(map, valueFilter);
        NginxLog nginxLog = JSON.parseObject(json, NginxLog.class);
        return nginxLog == null ? "" : nginxLog.toString();
    }
}
